package cn.jxwazx.sheinspringboot.utils;

import cn.jxwazx.sheinspringboot.exception.MyException;
import java.util.Map;
import org.dromara.hutool.json.jwt.JWT;
import org.dromara.hutool.json.jwt.JWTPayload;
import org.dromara.hutool.json.jwt.JWTUtil;

/**
 * @author LoveHuaHua
 * @date 2023年11月12日 22:55
 * @description believe in yourself
 */
public class JwtUtil {

    private static final byte[] key = "manggu_666".getBytes();

    /**
     * 转jwt
     * @param map
     * @return
     */
    public static String encrypt(Map<String, Object> map) {
        map.put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
        return JWTUtil.createToken(map, key);
    }

    /**
     * 解析token
     * @param token
     * @return
     */
    public static JWTPayload parse(String token) {
        boolean verify = JWTUtil.verify(token, key);
        CheckUtil.checkTrue(verify,new MyException("Token解析失败"));
        JWT jwt = JWTUtil.parseToken(token);
        return jwt.getPayload();
    }

}
